package com.cj.mobile.iabp.mvp.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cj.mobile.iabp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 下拉框适配器
 */
public class DropDownBoxAdapter extends RecyclerView.Adapter<DropDownBoxAdapter.ViewHolder> {

    private List<String> originalData;
    private List<String> filteredData;
    private OnItemClickListener listener;
    private String selectedItem;

    public interface OnItemClickListener {
        void onItemClick(String installer);
    }

    public DropDownBoxAdapter(List<String> data, OnItemClickListener listener) {
        this.originalData = new ArrayList<>(data);
        this.filteredData = new ArrayList<>(data);
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_drop_down_box, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String installer = filteredData.get(position);
        holder.bind(installer, selectedItem, listener);
    }

    @Override
    public int getItemCount() {
        return filteredData.size();
    }

    /**
     * 筛选数据
     */
    public void filter(String query) {
        filteredData.clear();
        if (TextUtils.isEmpty(query)) {
            // 如果查询为空，显示所有数据
            filteredData.addAll(originalData);
        } else {
            // 筛选包含查询文本的数据
            String lowerCaseQuery = query.toLowerCase();
            for (String item : originalData) {
                if (item.toLowerCase().contains(lowerCaseQuery)) {
                    filteredData.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 设置选中项
     */
    public void setSelectedItem(String item) {
        this.selectedItem = item;
        notifyDataSetChanged();
    }

    /**
     * 显示所有数据（清空筛选）
     */
    public void showAllData() {
        filteredData.clear();
        filteredData.addAll(originalData);
        notifyDataSetChanged();
    }

    /**
     * 更新数据源
     */
    public void updateData(List<String> newData) {
        this.originalData = new ArrayList<>(newData);
        showAllData();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView installerNameTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            installerNameTextView = itemView.findViewById(R.id.installerNameTextView);
        }

        public void bind(String installer, String selectedItem, OnItemClickListener listener) {
            installerNameTextView.setText(installer);

            // 显示选中状态
//            if (installer.equals(selectedItem)) {
//                installerNameTextView.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.text_l_bg_color));
//            } else {
//                installerNameTextView.setTextColor(ContextCompat.getColor(itemView.getContext(), android.R.color.black));
//            }

            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(installer);
                }
            });
        }
    }
}